﻿using com.guojin.core.io.message;
using com.guojin.mj.net;
using com.guojin.mj.net.message.game;
using com.guojin.mj.net.message.login;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class LoginController
{
    private static LoginController instance;
    public static LoginController GetInstance()
    {
        if (instance == null)
        {
            instance = new LoginController();
        }
        return instance;
    }
    public void OnReceiveMsg7166(string msg, Message data)
    {
        UpdaClubLmInfor lr = (UpdaClubLmInfor)data;
        GameData.GetInstance().lianMengDic.Clear();        GameData.GetInstance().clubDic.Clear();        for (int i = 0; i < lr.clubList.Count; i++)        {            GameData.GetInstance().clubDic.Add(lr.clubList[i].key, int.Parse(lr.clubList[i].value));        }        for (int i = 0; i < lr.lianMengList.Count; i++)        {            GameData.GetInstance().lianMengDic.Add(lr.lianMengList[i].key, int.Parse(lr.lianMengList[i].value));        }
    }
    public void OnReceiveMsg79(string msg, Message data)
    {
        Debug.Log("服务器返回登录错误");
        //PlayerPrefs.DeleteAll();
        //LoginAndShare.Controller.OnClickWXLogin();
    }

    public void OnReceiveMsg710(string msg, Message data)
    {
        Debug.Log("7,10");
        LoginRet lr = (LoginRet)data;
        //保存token
        PlayerPrefs.SetString(DefaultKeyConst.VisitorTokenKey, lr.token);
        Debug.Log("服务器返回token:" + lr.token);

        //保存代理等级和房卡数
        GameData.GetInstance().level = lr.level;
        GameData.GetInstance().gold = lr.gold;
        GameData.GetInstance().userID = lr.id.ToString();

        //这里加上玩家IP
        GameData.GetInstance().avatar = lr.avatar;
        Debug.Log(GameData.GetInstance().avatar);
        GameData.GetInstance().userIP = lr.ip;
        GameData.GetInstance().userName = lr.name;
        GameRoomModel.GetInstance().RoomID = lr.roomCheckId;

        GameData.GetInstance().Zhuan = lr.zhuan;
        GameData.GetInstance().clubDic.Clear();
        GameData.GetInstance().lianMengDic.Clear();

        for (int i = 0; i < lr.clubList.Count; i++)
        {
            GameData.GetInstance().clubDic.Add(lr.clubList[i].key, int.Parse(lr.clubList[i].value));
        }

        for (int i = 0; i < lr.lianMengList.Count; i++)
        {
            GameData.GetInstance().lianMengDic.Add(lr.lianMengList[i].key, int.Parse(lr.lianMengList[i].value));
        }

        CheckActiveScene();
        CheckNetConnectPage();
    }

    public void OnReceiveMsg717(string msg, Message data)
    {

        Debug.Log("服务器返回重复登录");
        if (SceneManager.GetActiveScene().name == "SHLogin")
        {
            PlayerPrefs.SetString(DefaultKeyConst.VisitorTokenKey, "");
            GameObject obj =GameObject.Find("LoginPage(Clone)");
            if (obj)
            {
                SocketMgr.GetInstance().isNetOpen = false;
                obj.GetComponent<LoginPage>().InformationSetting();
            }
        }
        else if (SceneManager.GetActiveScene().name == "SHGameHall")
        {
            GameObject obj_0 = GameObject.Find("NetConnectPage(Clone)");
            if (obj_0)
            {
                UnityEngine.Object.DestroyImmediate(obj_0);
            }
            UIManager.ChangeUI(UIManager.PageState.NetConnectPage, (GameObject obj) =>
            {
                obj.GetComponent<NetConnectPage>().InformationSetting();
            });
            //TooL.ShowTipsPage("连接服务器失败，需要重新登陆！", TooL.TipsPageType.noCancelPage,
            //() =>
            //{
            //    LoginAndShare.Controller.backLoginPage();

            //});
        }

    }

    public void Go() {
        CheckActiveScene();
    }
    private void CheckActiveScene()
    {
        if (SceneManager.GetActiveScene().name == "SHLogin")
        {
            GameObject obj = GameObject.Find("LoginPage(Clone)");
            if (obj)
            {
                GameObject.Find("StaticSource").GetComponent<MainLogic>().ChangeState((int)GameCore.UIState.UIState_SHLoadingState);
            }
        }
        else if (SceneManager.GetActiveScene().name == "SHGameHall" )
        {
            if ( GameRoomModel.GetInstance().RoomID != null)
            {
                MessageManager.GetInstance().SendMsg126();
            }
            else
            {
                GameController.GetInstance().DeleteAllExceptGameHall();
            }       
        }
    }

    private  void CheckNetConnectPage()
    {
        if (GameObject.Find("NetConnectPage(Clone)"))
        {
            GameObject.Find("NetConnectPage(Clone)").GetComponent<NetConnectPage>().CloseBtnClick();
            GameObject.Find("StaticSource").GetComponent<WorldTime>().StartSendPingCountDown();    //开始发送心跳包
        }
    }
}
